package juc.completablefuture;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName UseCompletableFuture
 * @PackageName completablefuture
 * @projectName myThread
 * @Description 
 * @Author games
 * @Date 2025/8/27 下午1:20
 * @Version 1.0
 */
public class CfExceptionally {
    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newFixedThreadPool(5);

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                // 处理中断异常
            }
            if (new Random().nextInt(10) % 2 == 0) {
                int i = 12 / 0; // 故意制造除零异常
            }
            System.out.println("执行结束！");
            return "test";
        });

        future.exceptionally(t -> {
            System.out.println("执行失败: " + t.getMessage());
            return "异常xxxx";
        }).join();
        executorService.shutdown();
    }


}
